package com.example.administrator.xiaobo.activity;

import android.content.Intent;
import android.content.res.Configuration;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.example.administrator.xiaobo.R;
import com.example.administrator.xiaobo.adapter.BoAdapter;
import com.example.administrator.xiaobo.jsonbean.DadaMovie;
import com.example.administrator.xiaobo.jsonbean.MovieJsonBean;
import com.example.administrator.xiaobo.jsonbean.MovieUpData;
import com.example.administrator.xiaobo.url.URLAdress;
import com.example.administrator.xiaobo.utils.BaseCallback;
import com.example.administrator.xiaobo.utils.OkHttpHelper;
import com.google.gson.Gson;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;

import java.util.ArrayList;
import java.util.List;

import io.vov.vitamio.MediaPlayer;
import io.vov.vitamio.Vitamio;
import io.vov.vitamio.widget.VideoView;

public class BoActivity extends AppCompatActivity  implements MediaPlayer.OnInfoListener, MediaPlayer.OnBufferingUpdateListener{
    //视频地址
    private String path;
    private Uri uri;
    private ProgressBar pb;
    private TextView downloadRateView, loadRateView;
    private CustomMediaController mCustomMediaController;
    private VideoView mVideoView;
    private LinearLayout layout;

    private List<DadaMovie> dadaMovieList;
    private RecyclerView recyclerView;
    private BoAdapter boAdapter;
    private OkHttpHelper okHttpHelper = OkHttpHelper.getInstance();

   private int id;
    private int recordparams;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        //定义全屏参数
        int flag = WindowManager.LayoutParams.FLAG_FULLSCREEN;
        //获得当前窗体对象
        Window window = BoActivity.this.getWindow();
        //设置当前窗体为全屏显示
        window.setFlags(flag, flag);
        //必须写这个，初始化加载库文件
        Vitamio.isInitialized(this);
        //设置视频解码监听

        setContentView(R.layout.activity_bo);

        Intent intent=getIntent();
        id=intent.getIntExtra("id",0);
        recordparams=intent.getIntExtra("recordparams",0);

        recyclerView= (RecyclerView) findViewById(R.id.recycler_bo);
        dadaMovieList=new ArrayList<>();
        initDataMovieUp();
        initView();
     //nihaoasaddsdddssss

    }

    private void initDataMovieUp() {
        String url= URLAdress.Bo_URL+"&id="+id+"&recordparams="+recordparams+"&tt="+System.currentTimeMillis();
        okHttpHelper.get(url, new BaseCallback() {
            @Override
            public void onBeforeRequest(Request request) {

            }

            @Override
            public void onFailure(Request request, Exception e) {

            }

            @Override
            public void onSuccess(String response) {

                Gson gson=new Gson();
                MovieUpData movieUpData=gson.fromJson(response,MovieUpData.class);
                dadaMovieList.add(movieUpData);
                doOtherHttp();
                path=movieUpData.getData().getStreamurl();
                initData(path);
            }

            @Override
            public void onError(Response response, int code) {

            }
        });



    }

    private void doOtherHttp() {
        okHttpHelper.get("http://hilive.baidu.com/service/api.do?ac=cateAnchorRandomList&column=0&id=43&tt=1486794673363", new BaseCallback() {
            @Override
            public void onBeforeRequest(Request request) {

            }

            @Override
            public void onFailure(Request request, Exception e) {

            }

            @Override
            public void onSuccess(String response) {

                Gson gson=new Gson();
                MovieJsonBean movieJsonBean=gson.fromJson(response,MovieJsonBean.class);
                dadaMovieList.add(movieJsonBean);
//                Log.e("ffff",dadaMovieList.size()+"");
//                for (DadaMovie dadaMovie : dadaMovieList) {
//                    if (dadaMovie instanceof MovieUpData){
//                        Log.e("GGGGGGGGG",((MovieUpData) dadaMovie).getData().getCatid()+"");
//                    }
//                }
                boAdapter = new BoAdapter(BoActivity.this, dadaMovieList);
                recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
                recyclerView.setAdapter(boAdapter);
            }

            @Override
            public void onError(Response response, int code) {

            }
        });
    }


    //初始化控件
    private void initView() {
        mVideoView = (VideoView) findViewById(R.id.buffer);
       mCustomMediaController=new CustomMediaController(this,mVideoView,this);
        mCustomMediaController.show(5000);

        mCustomMediaController.setVideoName("白火锅 x 红火锅");

        pb = (ProgressBar) findViewById(R.id.probar);
        downloadRateView = (TextView) findViewById(R.id.download_rate);
        loadRateView = (TextView) findViewById(R.id.load_rate);

    }

    //初始化数据
    private void initData(String path) {
        uri = Uri.parse(path);
        mVideoView.setVideoURI(uri);//设置视频播放地址

        mVideoView.setMediaController(mCustomMediaController);
        mVideoView.setVideoQuality(MediaPlayer.VIDEOQUALITY_HIGH);//高画质
        mVideoView.requestFocus();
        mVideoView.setOnInfoListener(this);
        mVideoView.setOnBufferingUpdateListener(this);
        mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mediaPlayer) {
                mediaPlayer.setPlaybackSpeed(1.0f);
            }
        });
    }

    @Override
    public boolean onInfo(MediaPlayer mp, int what, int extra) {

        switch (what) {
            case MediaPlayer.MEDIA_INFO_BUFFERING_START:
                if (mVideoView.isPlaying()) {
                    mVideoView.pause();
                    pb.setVisibility(View.VISIBLE);
                    downloadRateView.setText("");
                    loadRateView.setText("");
                    downloadRateView.setVisibility(View.VISIBLE);
                    loadRateView.setVisibility(View.VISIBLE);

                }
                break;
            case MediaPlayer.MEDIA_INFO_BUFFERING_END:
                mVideoView.start();
                pb.setVisibility(View.GONE);
                downloadRateView.setVisibility(View.GONE);
                loadRateView.setVisibility(View.GONE);
                break;
            case MediaPlayer.MEDIA_INFO_DOWNLOAD_RATE_CHANGED:
                downloadRateView.setText("" + extra + "kb/s" + "  ");
                break;
        }
        return true;
    }

    @Override
    public void onBufferingUpdate(MediaPlayer mp, int percent) {
        loadRateView.setText(percent + "%");
    }


    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        //屏幕切换时，设置全屏
        if (mVideoView != null){
            mVideoView.setVideoLayout(VideoView.VIDEO_LAYOUT_SCALE, 0);
        }
        super.onConfigurationChanged(newConfig);
    }

}

